package com.dl.mall.exception;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.dl.mall.utils.result.Result;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * sentinel异常
 * @author DL
 * @create 2022-04-07 10:49
 */
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class GlobalSentinelException {

    @ResponseBody
    @ExceptionHandler(FlowException.class)
    public Result handlerException(FlowException exception, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // 打印堆栈，以供调试
        System.out.println("sentinel异常异常---------------");
        exception.printStackTrace();

        // 返回给前端
        return Result.no("您访问太快了，休息一下吧~");
    }

}
